import com.dmotiko.lib.*
import com.dmotiko.util.*
import com.dmotiko.general.BaseClip;
import mx.transitions.*;
import mx.transitions.easing.*;

class ScrollerMovie
extends MovieClip {
	
	var targY;
	var dragger;
	var trigger;
	var theMask;
	var theText;
	var mcBarrita;
	var scrollAmount;
	
	public static function setScroll( theContent:MovieClip, theMask:MovieClip, dragger:MovieClip, bar:MovieClip, bHorizontal:Boolean ):Void {
		
		var trigger = dragger._trigger;
		var btn_up = dragger._btnup;
		var btn_down = dragger._btndown;
		var nStep = dragger._step ? dragger._step : 20;
		
		if (trigger) {
			dragger.hitArea = trigger;
			trigger._visible = false;
			trigger._clip = dragger;
			trigger.offsetX = dragger._x - trigger._x;
			trigger.offsetY = dragger._y - trigger._y;
			trigger.followStart = function(){
				this.onEnterFrame = function(){
					this._x = this._clip._x - this.offsetX;
					this._y = this._clip._y - this.offsetY;
				}
			}
			trigger.followEnd = function(){
				this._x = this._clip._x - this.offsetX;
				this._y = this._clip._y - this.offsetY;
				delete this.onEnterFrame;
			}
			
			trigger.onMotionFinished = function() {
				this.followEnd();
			}
		}
		
		if ( btn_up && !bHorizontal ) {
			btn_up.onRelease = function() {
				var nY:Number;
				if ( dragger._y - nStep > 0 ) nY = dragger._y - nStep;
				else if ( dragger._y > 0 ) nY = 0;
				if (dragger.tweenMove) dragger.tweenMove.stop();
				if ( nY == undefined ) return;
				dragger.tweenMove = new Tween(dragger, "_y", Regular.easeOut, dragger._y, nY, 0.3, true);
				if (trigger) {
					trigger.followStart();
					dragger.tweenMove.addListener( trigger );
				}
			}
		} else if ( btn_up && bHorizontal) {
			btn_up.onRelease = function() {
				var nX:Number;
				if ( dragger._x - nStep > 0 ) nX = dragger._x - nStep;
				else if ( dragger._x > 0 ) nX = 0;
				if (dragger.tweenMove) dragger.tweenMove.stop();
				if ( nX == undefined ) return;
				dragger.tweenMove = new Tween(dragger, "_x", Regular.easeOut, dragger._x, nX, 0.3, true);
				if (trigger) {
					trigger.followStart();
					dragger.tweenMove.addListener( trigger );
				}
			}
		}
		if ( btn_down && !bHorizontal ) {
			btn_down.onRelease = function() {
				var nY:Number;
				if ( dragger._y + nStep < theMask._height - dragger._height ) nY = dragger._y + nStep;
				else if ( dragger._y < theMask._height - dragger._height ) nY = theMask._height - dragger._height ;
				if (dragger.tweenMove) dragger.tweenMove.stop();
				if ( nY == undefined ) return;
				dragger.tweenMove = new Tween(dragger, "_y", Regular.easeOut, dragger._y, nY, 0.3, true);
				if (trigger) {
					trigger.followStart();
					dragger.tweenMove.addListener( trigger );
				}
			}
		} else if ( btn_down && bHorizontal) {
			btn_down.onRelease = function() {
				var nX:Number;
				if ( dragger._x + nStep < theMask._width - dragger._width ) nX = dragger._x + nStep;
				else if ( dragger._x < theMask._width - dragger._width ) nX = theMask._width - dragger._width ;
				if (dragger.tweenMove) dragger.tweenMove.stop();
				if ( nX == undefined ) return;
				dragger.tweenMove = new Tween(dragger, "_x", Regular.easeOut, dragger._x, nX, 0.3, true);
				if (trigger) {
					trigger.followStart();
					dragger.tweenMove.addListener( trigger );
				}
			}
		}
		
		//drag is restricted to the height of the mask
		dragger.onPress=function(){
			if ( bHorizontal ) this.startDrag(false, 0, this._y, theMask._width - this._width, this._y);
			else this.startDrag(false,this._x,0,this._x,theMask._height-this._height);
			if (dragger.tweenMove) dragger.tweenMove.stop();
			if (trigger) trigger.followStart();
		}
		//stop the drag
		dragger.onRelease=dragger.onReleaseOutside=function(){
			this.stopDrag();
			if (trigger) trigger.followEnd();
		}
		//set the mask for the text
		theContent.setMask(theMask);
		
		//init bar and dragger
		( bar instanceof BaseClip) ? bar.hide() : bar._visible = false;
		( dragger instanceof BaseClip) ? dragger.hide() : dragger._visible = false;
		btn_up._visible = btn_down._visible = false;
				
		//the scrolling animation
		theContent.onEnterFrame=function(){
			var check:Boolean;
			if (bHorizontal) check = (this._width <= theMask._width);
			else check = (this._height <= theMask._height);
					
			/* shows or hides the dragger and bar */
			if ( check ) {
				( bar instanceof BaseClip) ? bar.hide() : bar._visible = false;
				( dragger instanceof BaseClip) ? dragger.hide() : dragger._visible = false;
				btn_up._visible = btn_down._visible = false;
			} else {
				( bar instanceof BaseClip) ? bar.show() : bar._visible = true;
				( dragger instanceof BaseClip) ? dragger.show() : dragger._visible = true;
				btn_up._visible = btn_down._visible = true;
			}
						
			if ( bHorizontal ) {
				/* needs to check */
				var scrollAmount=(this._width-(theMask._width))/(theMask._width-dragger._width);
				var targX=-dragger._x*scrollAmount;
				this._x -= (this._x - targX) / 5;
				
			} else {
				var scrollAmount=(this._height-(theMask._height))/(theMask._height-dragger._height);
				var targY=-dragger._y*scrollAmount;
				this._y-=(this._y-targY)/5;	
			}
			
		}
		
		
	}
	
	public static function releaseScroll( theContent:MovieClip, dragger:MovieClip, bar:MovieClip ):Void {
		
		( bar instanceof BaseClip) ? bar.hide() : bar._visible = false;
		( dragger instanceof BaseClip) ? dragger.hide() : dragger._visible = false;
		
		delete theContent.onEnterFrame;
		if (dragger._trigger) dragger._trigger.followEnd();		
		dragger._btnup._visible = dragger._btndown._visible = false;
	}
	
	function ScrollerMovie(){
		
		var root = this;
		
		trigger = dragger._trigger;
		if (trigger) {
			dragger.hitArea = trigger;
			trigger._visible = false;
			trigger._clip = dragger;
			trigger.offsetX = dragger._x - trigger._x;
			trigger.offsetY = dragger._y - trigger._y;
			trigger.followStart = function(){
				this.onEnterFrame = function(){
					this._x = this._clip._x - this.offsetX;
					this._y = this._clip._y - this.offsetY;
				}
			}
			trigger.followEnd = function(){
				delete this.onEnterFrame;
			}
		}
		
		//code by Billy T

		//set a variable
		targY=0;
		//set the x position of the dragger
		dragger._x = theMask._width + 5;
		mcBarrita._x = dragger._x + (dragger._width - mcBarrita._width) / 2;
		//set the drag action of the dragger
		//drag is restricted to the height of the mask
		dragger.onPress=function(){
			startDrag(this, false, this._x, 0, this._x, root.theMask._height - this._height);
			if (trigger) trigger.followStart();
		}
		//stop the drag
		dragger.onRelease=dragger.onReleaseOutside=function(){
			stopDrag();
			if (trigger) trigger.followEnd();
		}
		//set the mask for the text
		theText.setMask(theMask);
		
		root.mcBarrita._visible = root.dragger._visible = false;
		
		//the scrolling animation
		theText.onEnterFrame=function(){
			root.mcBarrita._visible = root.dragger._visible = !(this._height <= root.theMask._height);
			/*set a variable 
			this variable basically stores info regarding what fraction of the total text 
			is being displayed through the mask and ensures that dragging the dragger
			from top to bottom will reveal all the text.
			this allows you to change the amount of text and the scroller will update itself
			*/
			root.scrollAmount=(this._height-(root.theMask._height/*/1.3*/))/(root.theMask._height-root.dragger._height);
			//set a new target y position
			root.targY=-root.dragger._y*root.scrollAmount;
			//set the y of the text to 1/5 of the distance between its current y and the target y
			
			//change the 5 to a lower number for faster scrolling or a higher number for slower scrolling
			this._y-=(this._y-root.targY)/5;
		}
	}
		
}